<?php

/*
 * 代理商用户类
 */

class AgencyUser {

	//代理商基本信息
	private $agency_user_id,
			$email,
			$name,
			$address,
			$balance,
			$qq_no,
			$phone;

	public function __construct($registry) {
		$this->config = $registry->get('config');
		$this->db = $registry->get('db');
		$this->request = $registry->get('request');
		$this->session = $registry->get('session');

		//如果登陆过，并且session中保存有信息
		if (isset($this->session->data['agency_user_id']) && is_numeric($this->session->data['agency_user_id'])) {
			$sql = 'SELECT * FROM `rr1_agency_user` WHERE `agency_user_id`=:agency_user_id AND `status`=1';
			$params = array(
				':agency_user_id' => (int) $this->session->data['agency_user_id']
			);
			$row = $this->db->query($sql, $params)->row;
			if (!empty($row)) {
				$this->agency_user_id = (int) $row['agency_user_id'];
				$this->email = $row['email'];
				$this->address = $row['address'];
				$this->balance = doubleval($row['balance']);
				$this->qq_no = $row['qq_no'];
				$this->phone = $row['phone'];
			} else {
				$this->logout();
			}
		}
	}

	/*
	 * login
	 * 代理商登陆
	 * @param string $email email地址
	 * @param string $password 密码
	 * @return 若登陆成功，返回true,否则返回false
	 */
	public function login($email, $password) {
		$sql = 'SELECT * FROM `rr1_agency_user` WHERE email = :email AND password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1(:password)))))  AND status = 1';
		$params = array(
			':email' => $this->db->escape($email),
			':password' => $this->db->escape($password)
		);
		$row = $this->db->query($sql, $params)->row;
		if (!empty($row)) {
			$this->agency_user_id = (int) $row['agency_user_id'];
			$this->email = $row['email'];
			$this->address = $row['address'];
			$this->balance = doubleval($row['balance']);
			$this->qq_no = $row['qq_no'];
			$this->phone = $row['phone'];
			$this->session->data['agency_user_id'] = $this->agency_user_id;
			return true;
		} else {
			$this->logout();
		}
		return false;
	}

	/*
	 * isLogin
	 * 判断是否已登陆
	 * @return 若登陆，返回true。未登录，返回false
	 */
	public function isLogin() {
		if(isset($this->session->data['agency_user_id'])){
			return true;
		}else{
			return false;
		}
	}
	
	/*
	 * logout
	 * 注销当前用户
	 * @return void
	 */
	public function logout() {
		if(isset($this->session->data['agency_user_id'])){
			unset($this->session->data['agency_user_id']);
		}
		$this->agency_user_id = '';
		$this->email ='';
		$this->address = '';
		$this->balance = '';
		$this->qq_no = '';
		$this->phone = '';
	}
	
	public function getAgencyUserId() {
		return $this->agency_user_id;
	}

	public function getEmail() {
		return $this->email;
	}

	public function getName() {
		return $this->name;
	}

	public function getAddress() {
		return $this->address;
	}

	public function getBalance() {
		return $this->balance;
	}

	public function getQqNo() {
		return $this->qq_no;
	}

	public function getPhone() {
		return $this->phone;
	}


}
